﻿@page "/snrtable"
@attribute [TabItemOption(Text = "传感器列表")]

@using BootstrapBlazor.Components
@using TrackSystem.Entities.Dtos.Sensor;
@using TrackSystem.Entities.Extensions

<PageTitle>传感器列表</PageTitle>

<Table TItem="SensorWithChannelDto"
       IsPagination="true" PageItemsSource="@PageItemsSource"
       IsStriped="true" IsBordered="true" IsMultipleSelect="false" EditDialogIsDraggable="true"
       ShowToolbar="true" ShowExtendButtons="true"
       ShowSkeleton="true"
       IsExtendButtonsInRowHeader="true" ShowSearch="true"
       OnQueryAsync="@OnQueryAsync"
       OnSaveAsync="@OnSaveAsync"
       OnDeleteAsync="@OnDeleteAsync">

    <TableColumns>
        <TableColumn @bind-Field="@context.Id" Sortable="true" Width="20" />
        <TableColumn @bind-Field="@context.Name" Sortable="true" Width="100">
            <Template Context="value">
                <div class="d-flex">
                    @{
                        var row = value.Row;
                    }
                    <div class="ps-2">
                        <div>@value.Value</div>
                        <div class="user-demo-address">@row.Description</div>
                    </div>
                </div>
            </Template>
        </TableColumn>

        <TableColumn @bind-Field="@context.Station" Width="50"></TableColumn>
        <TableColumn @bind-Field="@context.Eabled" Width="100">
            <Template Context="value">
                @{
                    var row = value.Row;
                }
                <Switch IsDisabled="true" Value="@row.Eabled" OnColor="Color.Success" OnText="使能" OffText="取消"></Switch>
            </Template>
        </TableColumn>

        <TableColumn @bind-Field="@context.RO_Address" Text="地址情况">
            <Template Context="value">
                @{
                    var row = value.Row;
                    string msg;
                    msg = row.RO_Length == 0 ? "无只读寄存器," : $"只读: {row.RO_Address} - {row.RO_Address + row.RO_Length - 1},";
                    msg += row.RW_Length == 0 ? " 无读写寄存器" : $" 读写: {row.RW_Address} - {row.RW_Address + row.RW_Length - 1}";
                }
                @msg
            </Template>
        </TableColumn>

        <TableColumn @bind-Field="@context.Channel">
            <Template Context="value">
                <div class="d-flex">
                    @{
                        var row = value.Row.Channel;
                    }
                    <div class="ps-2">
                        <div>@row?.Name (@row?.PortCategory.ToString() / @row?.Protocol.ToString())</div>
                        <div class="user-demo-address">
                            @{
                                var chn = value.Row.Channel;
                                string msg = chn?.PortCategory switch
                                {
                                    PortCategory.UDP => $"远程:{chn.Ip}:{chn.Port},本地:{chn.localIp}:{chn.localPort}",
                                    PortCategory.SRIAL => $"{chn.COMn},{chn.Baudrate},{chn.SerialParams}",
                                    _ => $"{chn?.Ip}:{chn?.Port}"
                                };
                            }
                            @msg
                        </div>
                    </div>
                </div>
            </Template>

        </TableColumn>

        <TableColumn @bind-Field="@context.Equip">
            <Template Context="value">
                <div class="d-flex">
                    @{
                        var equip = value.Row.Equip;
                    }
                    <div class="ps-2">
                        <div>@equip?.Name</div>
                    </div>
                </div>
            </Template>
        </TableColumn>

    </TableColumns>

    <EditTemplate>
        <div class="row g-3 form-inline">
            <div class="col-12 col-sm-6">
                <BootstrapInput @bind-Value="@context.Name" Placeholder="传感器名称" maxlength="20"></BootstrapInput>
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInput @bind-Value="@context.Description" Placeholder="传感器描述" maxlength="50"></BootstrapInput>
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInputNumber @bind-Value="@context.Station" Placeholder="站地址"></BootstrapInputNumber>
            </div>
            <div class="col-12 col-sm-6">
                <Switch @bind-Value="@context.Eabled" OnColor="Color.Success" OnText="启动使能" OffText="启动取消"></Switch>
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInputNumber @bind-Value="@context.RO_Address" Placeholder="只读寄存器起始地址"></BootstrapInputNumber>
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInputNumber @bind-Value="@context.RO_Length" Placeholder="只读寄存器长度"></BootstrapInputNumber>
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInputNumber @bind-Value="@context.RW_Address" Placeholder="读写寄存器起始地址"></BootstrapInputNumber>
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInputNumber @bind-Value="@context.RW_Length" Placeholder="读写寄存器长度"></BootstrapInputNumber>
            </div>
            <div class="col-12 col-sm-12">
                <RadioList Items=@lstStation @bind-Value=@context.channelID Placeholder="通信站号"></RadioList>
            </div>

            <div class="col-12 col-sm-12">
                <RadioList Items=@lstEquip @bind-Value=@context.equipId Placeholder="所属设备"></RadioList>
            </div>

        </div>
    </EditTemplate>

</Table>
